package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.VendorGoodsTagMapper;
import org.linlinjava.litemall.db.domain.LitemallGoodsProduct;
import org.linlinjava.litemall.db.domain.VendorGoodsTag;
import org.linlinjava.litemall.db.domain.VendorGoodsTagExample;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class VendorGoodsTagService {
    @Resource
    private VendorGoodsTagMapper vendorGoodsTagMapper;

    /**
     * 查询商品的标签列表
     * @return
     */
    public List<VendorGoodsTag> queryByGoodsId(Long goodsId) {
        VendorGoodsTagExample example = new VendorGoodsTagExample();
        example.or().andGoodsIdEqualTo(goodsId);
        return vendorGoodsTagMapper.selectByExample(example);
    }

    public List<VendorGoodsTag> findByTagId(Long tagId) {
        VendorGoodsTagExample example = new VendorGoodsTagExample();
        example.or().andTagIdEqualTo(tagId);
        return vendorGoodsTagMapper.selectByExample(example);
    }

    /**
     * 添加标签
     * @param tag
     * @return
     */
    public boolean add(VendorGoodsTag tag) {
        return vendorGoodsTagMapper.insertSelective(tag) > 0;
    }

    /**
     * 删除标签
     * @param id
     * @return
     */
    public boolean deleteById(long id) {
        VendorGoodsTagExample example = new VendorGoodsTagExample();
        example.or().andIdEqualTo(id);
        return vendorGoodsTagMapper.deleteByPrimaryKey(id) > 0;
    }

    public VendorGoodsTag findById(Long id) {
        return vendorGoodsTagMapper.selectByPrimaryKey(id);
    }

    /**
     * 更新标签
     * @param goodsTag
     * @return
     */
    public int updateById(VendorGoodsTag goodsTag) {
        return vendorGoodsTagMapper.updateByPrimaryKeySelective(goodsTag);
    }

    /**
     * 根据商品id删除标签
     * @param gid
     * @return
     */
    public boolean deleteByGid(Long gid) {
        VendorGoodsTagExample example = new VendorGoodsTagExample();
        example.or().andGoodsIdEqualTo(gid);
        return vendorGoodsTagMapper.deleteByExample(example) > 0;
    }

    /**
     * 根据tagId查询
     * @param tagId
     * @return
     */
    public List<VendorGoodsTag> queryByTagId(Long tagId, Integer page, Integer size) {
        VendorGoodsTagExample example = new VendorGoodsTagExample();
        example.or().andTagIdEqualTo(tagId);
        PageHelper.startPage(page, size);
        return vendorGoodsTagMapper.selectByExample(example);
    }
}
